<?php
class Staff extends CI_Controller{
	function __construct(){
		parent::__construct();
		//判断session
		if (!$this->session->userdata('name')){
			header("Location: /");
		}
	}
	function index(){
		$this->load->model('staff_model');
		//搜索内容
		$key = $this->input->get('key');
		$options = $this->input->get('options');
		$options = $options?$options:1;
		$offset = $this->input->get('offset');//得到分页的偏移量

		$where = array(
			'name' => $key,
			'status' => $options,
		);
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/staff/?key='.$key.'&options='.$options;
		$page_config['total_rows'] = $this->staff_model->get_all_rows($where);;
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$staff_list = $this->staff_model->get_staff_by_page($page_config['per_page'],$offset,$where);
//echo '<pre>';print_r($staff_list);exit();
		//查找内容
		$data['sidebar'] = array('office','staff');
		$data['query'] = $staff_list;
		$data['page'] = $pager_link;
		$data['options'] = $options;
		$data['key'] = $key;

		load_view('staff',$data);
	}
	
	function new_staff(){
		$data['sidebar'] = array('office','staff');
		$data['query'] = '';
		load_view('staff_file',$data);
	}
	
	function modify_staff($id){
		$this->load->model('staff_model');
		$obj = new Staff_model($id);
		$data['sidebar'] = array('office','staff');
		$data['query'] = array(
			'id' => $id,
			'name' => $obj->name,
			'department' => $obj->department,
			'job' => $obj->job,
			'extension' => $obj->extension,
			'mobile' => $obj->mobile,
			'email' => $obj->email,
			'status' => $obj->status,
		);
		load_view('staff_file',$data);
	}
	
	function execute(){
		$id = $this->input->post('id');
		$name = $this->input->post('name');
		$department = $this->input->post('department');
		$job = $this->input->post('job');
		$extension = $this->input->post('extension');
		$mobile = $this->input->post('mobile');
		$email = $this->input->post('email');
		$status = $this->input->post('status');
		
		$this->load->model('staff_model');
		$obj = new Staff_model($id);
		$obj->name = trim($name);
		$obj->department = $department;
		$obj->job = trim($job);
		$obj->extension = trim($extension);
		$obj->mobile = trim($mobile);
		$obj->email = trim($email);
		$obj->status = $status;
		
		if($id){
			$obj->changed = time();
			$obj->update();
			l('2',$name,$id);
		}else{
			$obj->created = $obj->changed = time();
			$obj->status = '1';		
			l('1',$name,$obj->save());
		}
		
		header("Location: /staff");
	}
	
	function del_staff($id){
		$this->load->model('staff_model');
		$obj = new Staff_model($id);
		$obj->delete();
		l('3',$obj->name,$id);
		header("Location: /staff");
	}
	
	function ajax(){
		$data = $this->input->post('data');
		$data = explode('_',$data);
		$oid = $data[0];//操作id
		$cid = $data[1];//消耗品id
		
		$this->load->model('consumable_model');
		$obj = new Consumable_model($cid);
		$sid = $obj->sid;
		$obj->sid = '0';
		$obj->hid = '0';
		$obj->status = $oid=='5'?'0':'2';
		$obj->update();
		$staff = staff_options();
		$key = $obj->name."来自于员工".$staff[$sid];
		l($oid,$key,$sid,$cid,'2');
	}
	
	function table_print($id){
		$this->load->model('staff_model');
		$obj = new Staff_model($id);
		$this->load->model('consumable_model');
		$consumable = $this->consumable_model->get_consumable($id);
		$host = $parts = $device = $books = $h = array();
		foreach($consumable as $row){
			switch($row['categoryid']){
				case '0':
					$parts[] = $row;
					break;
				case '1':
					$host[] = $row;
					break;
				case '2':
					$device[] = $row;
					break;
				case '3':
					$books[] = $row;
					break;
			}
		}
		$host_ip = array();
		foreach($host as $row){
			$ip = $this->consumable_model->get_ip($row['id']);
			$row['ip'] = $ip?$ip[0]['ip']:'该主机IP未分配';
			$host_ip[] = $row;
		}
		
		foreach($host_ip as $row){
			$row['parts'] = array();
			foreach($parts as $r){
				if($r['hid']==$row['id']){
					$row['parts'][] = $r; 
				}
			}
			$h[] = $row;
		}
		
		$data['query'] = array(
			'name' => $obj->name,
			'department' => $obj->department,
			'job' => $obj->job,
			'extension' => $obj->extension,
			'mobile' => $obj->mobile,
			'email' => $obj->email,
			'host' => $h,
			'device' => $device,
			'books' => $books,
		);
		$this->load->view('print',$data);
	}
	
	function view(){
		$this->load->model('staff_model');
		//搜索内容
		$key = $this->input->get('key');
		$options = $this->input->get('options');
		$options = $options?$options:1;
		$offset = $this->input->get('offset');//得到分页的偏移量

		$where = array(
			'name' => $key,
			'status' => $options,
		);
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/staff/view/?key='.$key.'&options='.$options;
		$page_config['total_rows'] = $this->staff_model->get_all_rows($where);;
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$staff_list = $this->staff_model->get_view($page_config['per_page'],$offset,$where);
//echo '<pre>'; print_r($staff_list);exit();
		//查找内容
		$data['sidebar'] = array('view','staff/view');
		$data['query'] = $staff_list;
		$data['page'] = $pager_link;
		$data['options'] = $options;
		$data['key'] = $key;

		load_view('staff_view',$data);
	}
}